import { Body, Controller, HttpCode, Post } from '@nestjs/common';
import AuthService from '../service/auth.service';
import LoginDto from '../dto/LoginDto';
import R from 'libs/common/utils/R';
import RegisterDto from '../dto/RegisterDto';
import AuthTokenDto from '../dto/AuthTokenDto';
import EmailDto from '../dto/EmailDto';

@Controller('/auth')
export default class MsAuthController {
  constructor(private readonly authService: AuthService) {}

  @HttpCode(200)
  @Post('/applyToJoin')
  async applyToJoin(
    @Body() registerDto: RegisterDto,
  ): Promise<R<AuthTokenDto>> {
    const authToken = await this.authService.applyToJoin(registerDto);
    return R.ok(authToken);
  }

  @HttpCode(200)
  @Post('/register')
  async register(@Body() registerDto: RegisterDto): Promise<R<string>> {
    const userId = await this.authService.register(registerDto);
    return R.ok<string>(userId);
  }

  @HttpCode(200)
  @Post('/login')
  async login(@Body() loginDto: LoginDto): Promise<R<AuthTokenDto>> {
    const authToken = await this.authService.login(loginDto);
    return R.ok<AuthTokenDto>(authToken);
  }

  @HttpCode(200)
  @Post('/validateIdentity')
  async validateIdentity(
    @Body() data: { token: string; service: string; api: string },
  ): Promise<R<any>> {
    console.log(data);
    // await this.authService.validateIdentity(data.token);
    return R.ok(true);
  }

  @HttpCode(200)
  @Post('/getLoginCode')
  async getLoginCode(@Body() emailDto: EmailDto): Promise<R<any>> {
    await this.authService.getLoginCode(emailDto.email);
    return R.ok();
  }

  @HttpCode(200)
  @Post('/getRegisterCode')
  async getRegisterCode(@Body() emailDto: EmailDto): Promise<R<any>> {
    await this.authService.getRegisterCode(emailDto.email);
    return R.ok();
  }

  @HttpCode(200)
  @Post('/validateToken')
  async validateToken(@Body('token') token: string): Promise<R<any>> {
    const data = this.authService.validateToken(token);
    return R.ok(data);
  }

  @HttpCode(200)
  @Post('/validateRefToken')
  async validateRefToken(@Body('refToken') refToken: string): Promise<R<any>> {
    const data = this.authService.validateRefToken(refToken);
    return R.ok(data);
  }

  @HttpCode(200)
  @Post('/refreshToken')
  async refreshToken(
    @Body() authTokenDto: AuthTokenDto,
  ): Promise<R<AuthTokenDto>> {
    const data = this.authService.refreshToken(
      authTokenDto.token,
      authTokenDto.refToken,
    );
    return R.ok(data);
  }
}
